#include "window3.h"
#include "window2.h"
#include<QPainter>
#include"mybutton.h"
#include<QTimer>
#include "mywindow.h"
#include "towerbase.h"
window3::window3(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(1080,900);
    MyButton*db=new MyButton(":/db1.png");
    db->setParent(this);
    db->move(975,350);
    db->setIconSize(QSize(100000,100000));
    MyButton *back=new MyButton(":/back.png");
    back->setParent(this);

    back->move(5,5);
    back->setIconSize(QSize(40,40));
    connect(back,&MyButton::clicked,this,[=](){
       emit Back();
    });
    //MyButton *set_tower=new MyButton(":/22/button3.png");
    //set_tower->setParent(this);
    //set_tower->move(800,800);
    //set_tower->setIconSize(QSize(this->width(),this->height()));
    //connect(set_tower,&MyButton::clicked,this,&window3::set_tower);
    MyButton *tower_fire=new MyButton(":/wb.png");
    tower_fire->setParent(this);
    tower_fire->move(50,400);
    tower_fire->setIconSize(QSize(this->width(),this->height()));
    //connect(tower_fire,&MyButton::clicked,this,&window3::addMyObject2);
    QTimer *timer=new QTimer (this);
    connect(timer,&QTimer::timeout,this,&window3::updateScene);
     timer->start(10);

}
void window3::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix(":/map3.png");
 loadPos();
    painter.drawPixmap(0,0,1080,1200,pix);
    QPainter painttower(this);
    foreach (const TowerPosition &towerPos, towerPositionsList)
        towerPos.draw(&painter);
    foreach (towerbase *tower, towersList)
        tower->draw(&painter);



}

//void window3::set_tower(){
    //Tower *newtower=new Tower(QPoint(540,540),":/negeta1.png");
    //tower_list.push_back(newtower);
    //update();

//}

void window3::updatescreen(){
    update();
}
//void window3::addMyObject(){
    //MyObject*object=new MyObject(QPoint(100,100),QPoint(400,400),":/m2.gif");
    //object_list.push_back(object);
    //object->move();
    //update();
//}
void window3::updateScene(){
    foreach(MyObject2*object,object2_list)
        object->move();
    update();
}
//void window3::addMyObject2(){
   //MyObject2*object=new MyObject2(QPoint(325,325),QPoint(900,325),":/m2.gif");
    //object2_list.push_back(object);

    //update();
//}
//void window3::showInfo(QPainter*painter){
//    painter->save();
//    painter->setPen(Qt::red);
//    painter->drawText(QRect(100,100,100,50),QString("num1: %1 num2: %2").arg(num1).arg(num2));
//    painter->restore();
//}
void window3::loadPos()
{
    QPoint pos[] =
    {
        QPoint(570,600),
         QPoint(470,600),
         QPoint(370,600),
        QPoint(800,400),
        QPoint(800,600),
        QPoint(800,220)


    };
    int len	= sizeof(pos) / sizeof(pos[0]);

    for (int i = 0; i < len; ++i)
        towerPositionsList.push_back(pos[i]);
}

bool window3::ifbuy() const
{
    return true;
}
void window3::mousePressEvent(QMouseEvent *event)
{
    QPoint pressPos = event->pos();
    auto it = towerPositionsList.begin();
    while (it != towerPositionsList.end())
    {
        if (ifbuy() && it->containPoint(pressPos) && !it->hasTower())
        {
            it->setHasTower();
            QPoint centerpoint=it->centerPos();
            towerbase *tower1 = new towerbase(centerpoint,QPixmap(":/negeta2.png"));
            towersList.push_back(tower1);
            update();
            break;
        }
        ++it;
    }
}

